package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import no.ntnu.fp.model.Person;

public class LoginWindow extends JFrame {

	private static final long serialVersionUID = 1L;

	public LoginWindow() {
		super("Logg inn");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		add(new LoginPanel());
		pack();
		setVisible(true);
	}
	
	private JFrame getWindow() {
		return this;
	}
	
	private class LoginPanel extends JPanel {

		private static final long serialVersionUID = 1L;
		private JButton login, cancel, createUser;
		private JTextField username;
		private JPasswordField password;

		public LoginPanel() {
			login = new JButton("Logg inn");
			login.addActionListener(new LoginAction());
			cancel = new JButton("Avbryt");
			cancel.addActionListener(new CancelAction());
			createUser = new JButton("Opprett ny bruker");
			createUser.addActionListener(new CreateUserAction());
			
			username = new JTextField();
			username.setColumns(20);
			password = new JPasswordField();
			password.setColumns(20);
			
			JLabel usernameLabel = new JLabel("Username:");
			JLabel passwordLabel = new JLabel("Password:");
			
			JPanel inputPanel = new JPanel();
			GroupLayout layout = new GroupLayout(inputPanel);
			inputPanel.setLayout(layout);
			layout.setAutoCreateGaps(true);
			layout.setAutoCreateContainerGaps(true);		
			layout.setHorizontalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
					.addComponent(usernameLabel)
					.addComponent(passwordLabel))
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
					.addComponent(username)
					.addComponent(password))
			);
			layout.setVerticalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
					.addComponent(usernameLabel)
					.addComponent(username))
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
					.addComponent(passwordLabel)
					.addComponent(password))
			);
			
			JPanel buttonPanel = new JPanel();
			buttonPanel.add(login);
			buttonPanel.add(createUser);
			buttonPanel.add(cancel);
			
			setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
			add(inputPanel);
			add(buttonPanel);
			
			/////// TODO: Default login ///////
				username.setText("Togowa Sensei");
				password.setText("Seppuku");
			///////////////////////////////////
		}
		
		private class LoginAction implements ActionListener {
			public void actionPerformed(ActionEvent arg0) {
				String usrn = username.getText();
				char[] passw = password.getPassword();
				String s = "";
				for (int i = 0; i < passw.length; i++) {
					s += passw[i];
				}
				
				// TODO: Sjekk passord og brukernavn mot databasen //
				if(Person.checkLogin(usrn, s)){
					getWindow().setVisible(false);
					getWindow().dispose();
					new CalendarWindow();
				}
				/*else if ( usrn.equals("admin") ) {
			
						if (s.equals("admin") ) {
							getWindow().setVisible(false);
							getWindow().dispose();
							return;
						}
					}*/
				/////////////////////////////////////////////////////
				
			//	JOptionPane.showMessageDialog(getWindow(), "Wrong username or password!");
			}
		}
		
		private class CancelAction implements ActionListener {
			public void actionPerformed(ActionEvent arg0) {
				getWindow().setVisible(false);
				getWindow().dispose();
				System.exit(0);
			}
		}
		
		private class CreateUserAction implements ActionListener {
			public void actionPerformed(ActionEvent arg0) {
				new CreateUserWindow(getWindow());
			}
		}
	}
}


